﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 星云微讯科技有限公司
 * 后台-热门游戏
 *  Hot.php
 *  lichaoliang  2015-04-03
 */

class Hot extends Admin_Controller
{
    private $table = 'game_hot';

    public function __construct()
    {
        parent::__construct();
        $this->load->model('hot_model');
    }

    /**
     *  有奖游戏列表
     */
    public function index()
    {
        $params = intval($this->uri->segment(4));
        /* echo $params;
         exit; */
        if(!empty($params)){
            $page = $params;
        }else{
            $page = 1;
        }
        $this->load->library('pagination');
        $view['title'] = !empty($_GET['title'])?addslashes($_GET['title']):'';
        $view['client_id'] = !empty($_GET['client_id'])?intval($_GET['client_id']):0;
        $view['category_id'] = !empty($_GET['category_id'])?intval($_GET['category_id']):0;
        $view['admin'] = 'admin';
        $config['use_page_numbers'] = TRUE;
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->hot_model->total_rows($view);
        $config['per_page'] = 50;
        $config['base_url'] = site_url().'/admin/hot/index/';
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $config['cur_tag_open'] = '<b>';
        $config['cur_tag_close'] = '</b>';
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $this->pagination->initialize($config);
        $page=($page-1)*$config['per_page'];

        //获取数据。
        $arr = $this->hot_model->get_list($page,$config['per_page'],$view);
        $page_view =  $this->pagination->create_links();
        $data = array('data'=>$arr,'page_view'=>$page_view);
        $this->load->view('admin/hot/index.php',$data);
    }

    public function add(){
        $category = $this->comm_model->get_list(0,100,'category');
        $client = $this->comm_model->get_list(0,100,'client');
        $this->load->view('admin/hot/add.php',array('category'=>$category,'client'=>$client));
    }
    /**
     *  添加逻辑
     */
    public function process_add(){
        header("Content-type:text/html;charset=utf-8");
        $view['title'] = !empty($_POST['title'])?addslashes($_POST['title']):'';
        $view['description'] = !empty($_POST['description'])?addslashes($_POST['description']):'';
        $view['url'] = !empty($_POST['url'])?addslashes($_POST['url']):'';
        $view['category_id'] = !empty($_POST['category_id'])?intval($_POST['category_id']):0;
        $view['client_id'] = !empty($_POST['client_id'])?intval($_POST['client_id']):0;
        $view['status'] = !empty($_POST['status'])?intval($_POST['status']):0;
        $view['sort'] = !empty($_POST['sort'])?intval($_POST['sort']):0;
        $view['click'] = 100;

        $view['createtime'] = time();


        /* 图片上传 logo */
        $path  = str_replace("\\", "/", FCPATH);
        $config['upload_path'] =$path  . 'uploads/hot/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['encrypt_name'] = true;
        $config['remove_spaces'] = true;

        $this->load->library('upload', $config);
        $field_name = "picture"; // 表单名称  <?php echo form_open_multipart('upload/do_upload'); <input type="file" name="field_name" size="20" />


        if ( ! $this->upload->do_upload($field_name))
        {
            $error = $this->upload->display_errors();
            echo "<script type='text/javascript'>alert('$error');window.location.href='".site_url('admin/hot/add')."';</script>";
            exit;
        }
        else
        {
            $upload_data = $this->upload->data();
            //$view['picture'] = str_replace($path,'',$upload_data['full_path']);
            $view['picture'] = 'uploads/hot/'.$upload_data['file_name'];

            $id = $this->comm_model->insert($this->table,$view);
            if(!empty($id)){
                echo "<script type='text/javascript'>window.location.href='".site_url('admin/hot/index')."';</script>";
            }else{
                echo "<script type='text/javascript'>alert('添加失败');window.location.href='".site_url('admin/hot/index')."';</script>";
            }
        }
    }

    public function update($id){
        $category = $this->comm_model->get_list(0,100,'category');
        $client = $this->comm_model->get_list(0,100,'client');
        $view = $this->comm_model->get_one($id,$this->table);
        $this->load->view('admin/hot/update.php',array('category'=>$category,'client'=>$client,'view'=>$view));
    }

    public function process_update(){
        header("Content-type:text/html;charset=utf-8");
        $id = !empty($_POST['id'])?intval($_POST['id']):0;
        $view['title'] = !empty($_POST['title'])?addslashes($_POST['title']):'';
        $view['description'] = !empty($_POST['description'])?addslashes($_POST['description']):'';
        $view['url'] = !empty($_POST['url'])?addslashes($_POST['url']):'';
        $view['category_id'] = !empty($_POST['category_id'])?intval($_POST['category_id']):0;
        $view['client_id'] = !empty($_POST['client_id'])?intval($_POST['client_id']):0;
        $view['status'] = !empty($_POST['status'])?intval($_POST['status']):0;
        $view['sort'] = !empty($_POST['sort'])?intval($_POST['sort']):0;
        $field_name = "picture";
        if(!empty($_FILES[$field_name]['tmp_name'])) {
            /* 图片上传 logo */
            $path = str_replace("\\", "/", FCPATH);
            $config['upload_path'] = $path . 'uploads/hot/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config['encrypt_name'] = true;
            $config['remove_spaces'] = true;

            $this->load->library('upload', $config);
            // 表单名称  <?php echo form_open_multipart('upload/do_upload'); <input type="file" name="field_name" size="20" />


            if (!$this->upload->do_upload($field_name)) {
                $error = $this->upload->display_errors();
                echo "<script type='text/javascript'>alert('$error');window.location.href='" . site_url('admin/hot/update') . "';</script>";
                exit;
            } else {
                $upload_data = $this->upload->data();
                $view['picture'] = 'uploads/hot/'.$upload_data['file_name'];
            }
        }

        $rs = $this->comm_model->update($id,$view,$this->table);
        if(!empty($rs)){
            echo "<script type='text/javascript'>window.location.href='".site_url('admin/hot/index')."';</script>";
        }else{
            echo "<script type='text/javascript'>alert('修改失败');window.location.href='".site_url('admin/hot/index')."';</script>";
        }
    }
    // 删除
    public function delete($id){
        $rs = $this->comm_model->del($id,$this->table);
        if(!empty($rs)){
            echo "<script type='text/javascript'>window.location.href='".site_url('admin/hot/index')."';</script>";
        }else{
            echo "<script type='text/javascript'>alert('删除失败');window.location.href='".site_url('admin/hot/index')."';</script>";
        }
    }
}